package org.ninjarope.cinderstained.main.gameplay.definition.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public abstract class Pool<T> {

	protected final BlockingQueue<T> objects;

	protected final int poolSize;

	public abstract void init();

	public Pool(int size) {
		this.poolSize = size;
		this.objects = new ArrayBlockingQueue<T>(size, false);
		init();
	}

	public Pool() {
		this(100);
	}

	public T request() {
		try {
			return this.objects.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void release(T object) {
		try {
			this.objects.put(object);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void releaseAll() {
		this.objects.clear();
		this.init();
	}
}
